# -*- coding: utf-8 -*-
import maya.cmds as mc
from liberHook.BaseHook import BaseHook
from liberLibs.path import Path
import liberConfig


TEXTURE_NODE_ATTR_DICT = liberConfig.TEXTURE_NODE_ATTR_DICT


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        super(Hook, self).__init__(library, directory, ext, start, end)

    def execute(self):
        if not Path(self.location).isFolder():
            self.append_error("No location found.")
            return
        if not Path(self.location).hasChildren():
            self.append_error("No files found.")
            return
        files = Path(self.location).children()
        files = [f for f in files if Path(f).isFile()]
        file_name = files[0]
        file_node = mc.shadingNode("file", asTexture=1, isColorManaged=1, name="file")
        attr = "%s.%s" % (file_node, TEXTURE_NODE_ATTR_DICT.get("file"))
        mc.setAttr(attr, file_name, type="string")
